package com.qf.test.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.test.mapper.DtsAdminMapper;
import com.qf.test.mapper.DtsBrandMapper;
import com.qf.test.mapper.DtsCategoryMapper;
import com.qf.test.pojo.DtsBrand;
import com.qf.test.vo.CatVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

//请求网址: http://127.0.0.1:8083/admin/brand/list?page=1&limit=20&id=aaa&name=ssss&sort=add_time&order=desc
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private DtsBrandMapper dtsBrandMapper;
    @Autowired
    DtsAdminMapper dtsAdminMapper;
    @Autowired
    DtsCategoryMapper dtsCategoryMapper;

    @Override
    public IPage<DtsBrand> querbrand(Integer page, Integer limit, String id, String name, String sort, String order) {
        //创建分页对象
        Page<DtsBrand> page1 = new Page<>(page,limit);
        //封装查询对象
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        //创建查询条件
        if (!StringUtils.isEmpty(id)) {
            queryWrapper.like("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            //判断是否为升序
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                //降序
                queryWrapper.orderByDesc(sort);
            }
        }
        //调用dao层查询返回数据
        Page<DtsBrand> page2 = dtsBrandMapper.selectPage(page1, queryWrapper);
        return page2;
    }

    @Override
    public DtsBrand add(DtsBrand dtsBrand) {
        dtsBrand.setAddTime(new Date());
        dtsBrand.setUpdateTime(new Date());
        dtsBrand.setDeleted(false);
       dtsBrandMapper.insert(dtsBrand);
        return dtsBrand;
    }

    @Override
    public int update(DtsBrand dtsBrand) {
        dtsBrand.setUpdateTime(new Date());
        int count = dtsBrandMapper.updateById(dtsBrand);
        return count;
    }

    @Override
    public int deleted(DtsBrand dtsBrand) {
        dtsBrand.setDeleted(false);
        int count = dtsBrandMapper.updateById(dtsBrand);
        return count;
    }

    @Override
    public List<CatVo> findall() {
        List<CatVo> list = dtsAdminMapper.findall();
        return list;
    }

    /**
     * 查询二级目录
     * @return
     */
    @Override
    public List<CatVo> all2() {
        List<CatVo> list = dtsCategoryMapper.select2();
        return list;

    }

    /**
     * 查询一级目录
     * @return
     */
    @Override
    public List<CatVo> all1() {

        List<CatVo> list = dtsCategoryMapper.select1();
        return list;



    }



}
